# -*- coding: utf-8 -*-#
# ----------------------------------------*_*
# Author:       Chasetity
# Date:         2021/7/6
# ----------------------------------------*_*


import time
from appium import webdriver
from selenium.webdriver.common.by import By

from utils import get_element,input_text,element_is_exsit

#定义app相关参数字典
des_cap = {
    'platformName':'android',   #表示系统
    'platformVersion':'7.1.2',  #系统版本
    'deviceName':'127.0.0.1:62001',  #设备名称
    'appPackage':'com.ss.android.ugc.aweme',  #表示app包名
    'appActivity':'.splash.SplashActivity', #表示app界面名称

    'noReset':True,   # 记录app的session值，定义app是否重置
    'resetKeyboard':True,   # 重置设备键盘，解决中文输入问题01
    'unicodeKeyboard':True,   # 采用unicode编码格式，解决中文输入问题02
}

driver = webdriver.Remote('http://localhost:4723/wd/hub',des_cap)  # appium服务器地址，app相关参数字典
time.sleep(5)

# 点击个人中心
air_ele = By.ID,'com.ss.android.ugc.aweme:id/ik5'
get_element(driver,air_ele).click()
time.sleep(2)

# 判断app是否首次 打开或重置
search_btn = By.XPATH,'//*[@text="密码登录"]'
if element_is_exsit(driver,search_btn):
    # 点击密码登录
    get_element(driver,search_btn).click()
    time.sleep(2)

    # 点击同意协议
    ret_ele = By.ID, 'com.ss.android.ugc.aweme:id/gl0'
    get_element(driver, ret_ele).click()
    time.sleep(2)

    # 输入手机号
    index1 = By.XPATH, '请输入手机号'
    input_text(get_element(driver, index1), 13164956830)
    time.sleep(2)

    # 输入密码
    index2 = By.XPATH, '请输入密码'
    input_text(get_element(driver, index2), 'chasetity0116')
    time.sleep(2)

    # 点击登录
    input_element = By.CLASS_NAME, 'android.widget.LinearLayout'
    get_element(driver, input_element).click()
    time.sleep(2)
else:
    print('非首次打开')
    # 打印屏幕分辨率并截图
    print(driver.get_window_size())  # 屏幕分辨率
    driver.get_screenshot_as_file('./images/error.png')  # 截图保存
    time.sleep(2)


# 打开首页
a = By.XPATH,'首页'
get_element(driver,a).click()
time.sleep(2)

# 点击搜索
b = By.ID,'com.ss.android.ugc.aweme:id/dsp'
get_element(driver,b)
time.sleep(2)

# 点击输入
c = By.ID,'com.ss.android.ugc.aweme:id/et_search_kw'
input_text(get_element(driver,c),'Python')
time.sleep(2)

# 点击搜索
d = By.ID,'com.ss.android.ugc.aweme:id/jzt'
get_element(driver,d).click()
time.sleep(2)





time.sleep(8)
driver.quit()